package com.hmdp.service.impl;

import cn.hutool.core.lang.TypeReference;
import com.hmdp.common.Constants;
import com.hmdp.dto.Result;
import com.hmdp.entity.Shop;
import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.RedisCache;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
@RequiredArgsConstructor
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {
    private final RedisCache redisCache;

    @Override
    public Result queryTypeList() {
        String key = Constants.CACHE_SHOP_TYPE_KEY;
        TypeReference<List<ShopType>> typeReference = new TypeReference<List<ShopType>>() {};
        List<ShopType> typeList = redisCache.safeGet(key, typeReference, null,
                (query) -> this.query().orderByAsc("sort").list(),
                Constants.CACHE_DATA_TTL, TimeUnit.MINUTES);
        return Result.ok(typeList);
    }
}
